package com.dc.project.controller;

import com.dc.common.utils.StringUtil;
import com.dc.project.workflow.service.ActFlowService;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhuangcy
 * @date 2021/6/7
 * @description 模型编辑器控制器
 */
@Api(tags = "流程模型编辑器控制器")
@Slf4j
@Controller
public class ActFlowEditorController implements ModelDataJsonConstants {
    @Autowired
    private ActFlowService actFlowService;


    /**
     * 跳转编辑器页面
     *
     * @return
     */
    @ApiOperation("跳转编辑器页面")
    @GetMapping("/editor")
    public String editor(String modelId) {
        return StringUtil.format("modeler.html?modelId={}", modelId);
    }

    /**
     * 获取编辑器 Json
     *
     * @param modelId
     * @return
     */
    @ResponseBody
    @ApiOperation("获取编辑器Json")
    @GetMapping(value = "model/{modelId}/json", produces = "application/json;charset=utf-8")
    public ObjectNode getEditorJson(@PathVariable String modelId) {
        return actFlowService.getEditorJson(modelId);
    }

    /**
     * 保存模型资源
     *
     * @param modelId
     * @param values
     */
    @ResponseBody
    @ApiOperation("保存模型资源")
    @PutMapping(value = "model/{modelId}/save")
    @ResponseStatus(value = HttpStatus.OK)
    public void saveModel(@PathVariable String modelId, @RequestParam MultiValueMap<String, String> values) {
        actFlowService.saveModel(modelId, values);
    }

    /**
     * 获取模板集
     *
     * @return
     */
    @ResponseBody
    @ApiOperation("获取模板集")
    @GetMapping(value = "editor/{modelId}/stencilSet", produces = "application/json;charset=utf-8")
    public String getStencilSet(@PathVariable String modelId) {
        return actFlowService.getStencilSet(modelId);
    }


}
